package org.sklse.lab.controller;

import com.sun.org.apache.regexp.internal.RE;
import org.sklse.lab.bean.Commodity;
import org.sklse.lab.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("Commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @RequestMapping(value="findAll",method= RequestMethod.GET)
    public @ResponseBody Object findALL(){
        return commodityService.findAll();
    }

    @RequestMapping(value="findById",method=RequestMethod.GET)
    public Object findByCommodityId(String CommodityId){
        return commodityService.findById(CommodityId);
    }


    @RequestMapping(value="buy",method= RequestMethod.GET)
    public boolean Buy(int num,String commodityId){
        //在数据量比较大时，这样写效率很低
        Commodity commodity=commodityService.findById(commodityId);
        int beforeBuyAmount=commodity.getCommodityAmount();
        System.out.println(beforeBuyAmount);
        int afterBuyAmount=beforeBuyAmount-num;
        System.out.println(afterBuyAmount);
        commodity.setCommodityAmount(afterBuyAmount);//没有save
        int a=commodity.getCommodityAmount();
        System.out.println(a);
        commodityService.save(commodity);
        return true;
    }

}
